<?php
require_once LIBS_DIR . '/DatePicker.php';
class Admin_ZapasPresenter extends BasePresenter
{	
	public $tymy;
	public $hraci;
	public function renderDefault()
	{
		$zapas = new ZapasModel();
		$this->template->zapasy = $zapas->findAll();

	}
	
	public function renderNovy()
	{
		$form = $this['zapasForm'];
	}

	public function renderUpravit($id = 0)
	{	
		$form = $this['zapasForm'];
		
		if (!$form->isSubmitted()) {
			$zapas = new ZapasModel();
			$soutez = new SoutezModel();
			$zapas_kola = new ZapasKoloModel();
			$tym_db = new TymModel();
			$row = $zapas->findById($id)->fetch();
			$soutez_id = $zapas->tym_soutez_findById($row['tym_soutez_id_a'])->fetch();
			$kola = $zapas_kola->findBySoutezId($soutez_id['soutez_id'])->fetchAll();
			$tymy = $soutez->tym_soutez_findBySoutezId($soutez_id['soutez_id'])->fetchAll();
			
			$tymy_list = array();
			$tymy_list[''] = 'Zvolte tým';
			foreach($tymy as $tym){
				$tym_nazev = $tym_db->findById($tym['tym_id'])->fetch();
				$tymy_list[$tym['id']] = $tym_nazev['nazev'];
			}
			
			$kola_list = array();
			$kola_list[''] = 'Zvolte kolo';
			foreach($kola as $kolo){
				$kola_list[$kolo['id']] = $kolo['nazev'];
			}
		
			$row['soutez_id'] = $soutez_id['soutez_id'];
			$form['zapas_kola_id']->setItems($kola_list);
			$form['tym_soutez_id_a']->setItems($tymy_list);
			$form['tym_soutez_id_b']->setItems($tymy_list);
			
			if (!$row) {
				throw new NBadRequestException('Záznam nebyl nalezen.');
			}
			
			$form->setDefaults($row);
		}
	}
	
	public function renderSmazat($id = 0)
	{
		$zapas = new ZapasModel();
		$this->template->zapas = $zapas->findById($id)->fetch();
		if (!$this->template->zapas) {
			throw new NBadRequestException('Zápas nebyl nalezen.');
		}
	}
	
	protected function createComponentSmazatForm()
	{
		$form = new NAppForm;
		
		$form->addSubmit('delete', 'Smazat')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět');
		$form->onSubmit[] = callback($this, 'smazatFormSubmitted');
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	
	public function smazatFormSubmitted(NAppForm $form)
	{
		if ($form['delete']->isSubmittedBy()) {
			$zapas = new ZapasModel();
			$zapas->delete($this->getParam('id'));
			$this->flashMessage('Zápas byl odstraněn.');
		}

		$this->redirect('default');
	}
	
	protected function createComponentZapasForm($name)
	{
		$soutez = new SoutezModel();
		
		$form = new NAppForm($this, $name);
		
		$form->addText('cislo_zapasu', 'Číslo zápasu:')->addRule(NAppForm::FILLED, 'Zadejte číslo zápasu');
		$form->addDatePicker('datum', 'Datum:');
		
		$tym_items = $soutez->tym_soutez_findAll()->fetchPairs('soutez_id','tym_id');
		$soutez_items = $soutez->findAll()->fetchPairs('id','nazev');
		$form->addSelect("soutez_id", "Soutěž:", $soutez_items)->skipFirst('Zvolte soutěž');
		$form->addDependentSelectBox("zapas_kola_id", "Kolo:", $form["soutez_id"], callback($this, "getValuesZapasKola"))->addRule(NAppForm::FILLED, 'Zvolte kolo')->skipFirst('Zvolte kolo');
		if($this->isAjax())
			$form["zapas_kola_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		$form->addDependentSelectBox("tym_soutez_id_a", "Tým A:",$form["soutez_id"], callback($this, "getValuesTymSoutez"))->addRule(NAppForm::FILLED, 'Zvolte tým A')->skipFirst('Zvolte tým A');
		if($this->isAjax())
			$form["tym_soutez_id_a"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		$form->addDependentSelectBox("tym_soutez_id_b", "Tým B:",$form["soutez_id"], callback($this, "getValuesTymSoutez"))->addRule(NAppForm::FILLED, 'Zvolte tým B')->skipFirst('Zvolte tým B');
		if($this->isAjax())
			$form["tym_soutez_id_b"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		$form->addCheckbox('zverejnit', 'Zveřejnit tento zápas?');
		$form->addText('vysledek', 'Výsledek:');
		$form->addSubmit('save', 'Uložit')->getControlPrototype()->class('default');
		$form['save']->onClick[] = callback($this, "zapasFormSubmitted");		
		$form->addSubmit('cancel', 'Zpět')->setValidationScope(NULL)->getControlPrototype()->class('cancel');
		$form['cancel']->onClick[] = callback($this, "zapasFormSubmitted");
		
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		$this->template->form = $form;
		return $form;
	}
	public function zapasFormSubmitted($button)
	{	
		if(isset($button->form->httpData['save'])){
			$values = array(
				'cislo_zapasu'=>$button->form['cislo_zapasu']->value,
				'zapas_kola_id'=>$button->form['zapas_kola_id']->value,
				'zverejnit'=>$button->form['zverejnit']->value, 
				'tym_soutez_id_a'=>$button->form['tym_soutez_id_a']->value, 
				'tym_soutez_id_b' => $button->form['tym_soutez_id_b']->value,
				'datum' => $button->form['datum']->value,
				'vysledek' => $button->form['vysledek']->value
			);
			
			$id = (int) $this->getParam('id');
			$zapas = new ZapasModel();
			if ($id > 0) {
				$zapas->update($id, $values);
				$this->flashMessage('Zápas byl upraven.');
			} else {
				
				$zapas->insert($values);
				$this->flashMessage('Zápas byl vytvořen.');
			}
		}

		$this->redirect('default');
	}

	
	
	
	/*  ROZHODČÍ  */
	
	public function renderRozhodciSmazat($id = 0)
	{
		$rozhodci = new ZapasModel();
		$this->template->rozhodci = $rozhodci->zapas_rozhodci_findById($id)->fetch();
		if (!$this->template->rozhodci) {
			throw new NBadRequestException('Rozhodčí nebyl nalezen.');
		}
	}
	
	protected function createComponentRozhodciSmazatForm()
	{
		$form = new NAppForm;
		
		$form->addSubmit('delete', 'Smazat')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět');
		$form->onSubmit[] = callback($this, 'rozhodciSmazatFormSubmitted');
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	
	public function rozhodciSmazatFormSubmitted(NAppForm $form)
	{
		if ($form['delete']->isSubmittedBy()) {
			$rozhodci = new ZapasModel();
			$rozhodci->zapas_rozhodci_delete($this->getParam('id'));
			$this->flashMessage('Rozhodčí byl odebrán.');
		}

		$this->redirect('rozhodciDefault');
	}

	public function renderRozhodciDefault()
	{
		$rozhodci = new ZapasModel();
		$lide = new LideModel();
		$soutez = new SoutezModel();
		$rozhodci_all = $rozhodci->zapas_rozhodci_findAll()->fetchAll();
		foreach($rozhodci_all as $item){
			$tmp_zapas = $rozhodci->findById($item['zapas_id'])->fetch();
			$tmp_soutez = $soutez->rozhodci_soutez_findById($item['rozhodci_soutez_id'])->fetch();
			$tmp_lide = $lide->findById($tmp_soutez['lide_id'])->fetch();
			
			$item['cislo_zapasu'] = $tmp_zapas['cislo_zapasu'];
			$item['rozhodci_jmeno'] = $tmp_lide;
		}
		
		$this->template->rozhodci = $rozhodci_all;
	}
	
	protected function createComponentRozhodciForm($name)
	{
		$soutez = new SoutezModel();
		$form = new NAppForm($this, $name);
		$typ_items = array('hlavní'=>'hlavní','čarový'=>'čarový');
		$tym_items = $soutez->tym_soutez_findAll()->fetchPairs('soutez_id','tym_id');
		$soutez_items = $soutez->findAll()->fetchPairs('id','nazev');
		$form->addSelect("soutez_id", "Soutěž:", $soutez_items)->skipFirst('Zvolte soutěž');
		$form->addDependentSelectBox("zapas_kola_id", "Kolo:", $form["soutez_id"], callback($this, "getValuesZapasKola"))->skipFirst('Zvolte kolo');
		if($this->isAjax())
			$form["zapas_kola_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		$form->addDependentSelectBox("zapas_id", "Zápas:",$form["zapas_kola_id"], callback($this, "getValuesZapas"))->skipFirst('Zvolte zápas');
		if($this->isAjax())
			$form["zapas_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		$form->addDependentSelectBox("rozhodci_soutez_id", "Lidé:",$form["soutez_id"], callback($this, "getValuesLide"))->skipFirst('Vyberte rozhodčího');
		if($this->isAjax())
			$form["rozhodci_soutez_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		$form->addSelect('typ', 'Typ:', $typ_items)->skipFirst('Vyberte typ');
		$form->addSubmit('save', 'Uložit')->getControlPrototype()->class('default');
		$form['save']->onClick[] = callback($this, "rozhodciFormSubmitted");		
		$form->addSubmit('cancel', 'Zpět')->getControlPrototype()->class('cancel')->setValidationScope(NULL);
		$form['cancel']->onClick[] = callback($this, "rozhodciFormSubmitted");
		
		
		
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	public function rozhodciFormSubmitted($button)
	{	
		
		if(isset($button->form->httpData['save'])){
			$values = array(
				'zapas_id'=>$button->form['zapas_id']->value,
				'rozhodci_soutez_id'=>$button->form['rozhodci_soutez_id']->value, 
				'typ' => $button->form['typ']->value
			);
			$rozhodci = new ZapasModel();
			$rozhodci->zapas_rozhodci_insert($values);
			$this->flashMessage('Rozhodčí byl přidán.');
			
		}

		$this->redirect('rozhodciDefault');
	}
	
	
	/*  HRACI  */
	public function renderHraciPridat()
	{
		$zapas = new ZapasModel();
		$tym = new TymModel();
		$lide = new LideModel();
		$tym_soutez = new SoutezModel();
		$pozice = new PoziceModel();
		$this->template->vybrany_tym = $this->tymy;
		$this->template->vybrany_hrac = $this->hraci;
		$get_zapas = $zapas->findById($this->tymy)->fetch();
		if($get_zapas['tym_soutez_id_a'] != FALSE){
			$tymy_zapas = array($get_zapas['tym_soutez_id_a'],$get_zapas['tym_soutez_id_b']);
			$tymy = array();
			foreach($tymy_zapas as $item){

				$tym_id = $tym_soutez->tym_soutez_findById($item)->fetch();
				$tym_nazev = $tym->findById($tym_id['tym_id'])->fetch();
				array_push($tymy,$tym_nazev);
			}
			$this->template->tymy = $tymy;
		}
		
		$lide_id = $tym->tym_lide_findByTymId($this->hraci)->fetchAll();
		$seznam_lidi = array();
		
		foreach($lide_id as $clovek){
			$clovek_jmeno = $lide->findById($clovek['lide_id'])->fetch();
			$clovek_jmeno['tym_lide_id'] = $clovek['id'];
			$seznam_lidi[$clovek['id']] = $clovek_jmeno;
			
		}
		
		$this->template->pozice = $pozice->findAll()->fetchPairs('id','nazev');
		$this->template->hraci = $seznam_lidi;
		
	}
	public function renderHraciSmazat($id = 0)
	{
		$hraci = new ZapasModel();
		$this->template->hraci = $hraci->zapas_hrac_findById($id)->fetch();
		if (!$this->template->hraci) {
			throw new NBadRequestException('Hráč nebyl nalezen.');
		}
	}
	
	protected function createComponentHraciSmazatForm()
	{
		$form = new NAppForm;
		
		$form->addSubmit('delete', 'Smazat')->getControlPrototype()->class('default');
		$form->addSubmit('cancel', 'Zpět');
		$form->onSubmit[] = callback($this, 'hraciSmazatFormSubmitted');
		$form->addProtection('Potvrďte prosím tento formulář znovu.');
		return $form;
	}
	
	public function hraciSmazatFormSubmitted(NAppForm $form)
	{
		if ($form['delete']->isSubmittedBy()) {
			$hraci = new ZapasModel();
			$hraci->zapas_hrac_delete($this->getParam('id'));
			$this->flashMessage('Hráč byl odebrán.');
		}

		$this->redirect('hraciDefault');
	}

	public function renderHraciDefault()
	{
		$zapas = new ZapasModel();
		$tym = new TymModel();
		$lide = new LideModel();
		$pozice = new PoziceModel();
		$hraci_zapas_all = $zapas->zapas_hrac_findAll()->fetchAll();
		foreach($hraci_zapas_all as $item){
			$tmp_zapas = $zapas->findById($item['zapas_id'])->fetch();
			$item['cislo_zapasu'] = $tmp_zapas['cislo_zapasu'];
			
			$tmp_tym_lide = $tym->tym_lide_findById($item['tym_lide_id'])->fetch();
			$tmp_lide = $lide->findById($tmp_tym_lide['lide_id'])->fetch();
			$item['hrac_jmeno'] = $tmp_lide['jmeno'].' '.$tmp_lide['prijmeni'];
			
			$tmp_pozice = $pozice->findById($item['pozice_id'])->fetch();
			$item['pozice_nazev'] = $tmp_pozice['nazev'];
		}
		$this->template->hraci = $hraci_zapas_all;
	}
	
	protected function createComponentHraciForm($name)
	{
		$soutez = new SoutezModel();
		$form = new NAppForm($this, $name);
		$soutez_items = $soutez->findAll()->fetchPairs('id','nazev');
		$form->addSelect("soutez_id", "Soutěž:", $soutez_items)->skipFirst('Zvolte soutěž');
		$form->addDependentSelectBox("zapas_kola_id", "Kolo:", $form["soutez_id"], callback($this, "getValuesZapasKola"))->skipFirst('Zvolte kolo');
		if($this->isAjax())
			$form["zapas_kola_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");
		
		$form->addDependentSelectBox("zapas_id", "Zápas:",$form["zapas_kola_id"], callback($this, "getValuesZapas"))->skipFirst('Zvolte zápas')->getControlPrototype()->class('ajax');
		if($this->isAjax())
			$form["zapas_id"]->addOnSubmitCallback(callback($this, "invalidateControl"), "formSnippet");		
			
		return $form;
	}
	public function hraciFormSubmitted($button)
	{	
		if(isset($button->form->httpData['save'])){
			$values = array(
				'zapas_id'=>$button->form['zapas_id']->value,
				'rozhodci_soutez_id'=>$button->form['rozhodci_soutez_id']->value, 
				'typ' => $button->form['typ']->value
			);
			$rozhodci = new ZapasModel();
			$rozhodci->zapas_rozhodci_insert($values);
			$this->flashMessage('Rozhodčí byl přidán.');
			
		}

		$this->redirect('hraciDefault');
	}
	public function actionPridatHraceForm(){
		if(isset($_POST['back']))
			$this->redirect('hraciDefault');	
		$tym_lide = new ZapasModel();
		$zapas_hrac = $_POST;
			$data = array();
			$num = 0;
			$zapas = $zapas_hrac['zapas'];
			unset($zapas_hrac['save']);
			unset($zapas_hrac['zapas']);
			
			
			foreach($zapas_hrac as $k => $v){
				if($v != ''){
					$data[$num]['zapas_id'] = (int)$zapas;
					$data[$num]['pozice_id'] = (int)$v;	

					$data[$num]['tym_lide_id'] = (int)$k;

					$num++;
				}
			}
			foreach($data as $tmp){
				$tym_lide->zapas_hrac_insert($tmp);
			}
			$this->redirect('hraciPridat');
			
	}
	public function handleChange($cislo){
		$this->tymy = $cislo;
		if($this->isAjax()){
			$this->invalidateControl('pokus');
		}
	}
	public function handleUkazHrace($id){
		$this->hraci = $id;
		if($this->isAjax()){
			$this->invalidateControl('hraci');
		}
	}
	function getValuesZapasKola($form) {
		$soutez_id = $form["soutez_id"]->getValue();
		return dibi::query("SELECT id, nazev FROM dbo.Zapas_Kola WHERE soutez_id=%i", $soutez_id)->fetchPairs("id", "nazev");
		//return dibi::query("SELECT id, nazev FROM dbo.Zapas_Kola WHERE soutez_id=%i", $soutez_id)->fetchPairs("id", "nazev");
	}
	function getValuesTymSoutez($form) {
		$soutez_id = $form["soutez_id"]->getValue();
		$tym = new TymModel();
		$tmp_tym = dibi::query("SELECT [*] FROM dbo.Tym_Soutez WHERE soutez_id=%i", $soutez_id)->fetchAll();
		$tym_assoc = array();
		foreach($tmp_tym as $item){
			$tmp_tym_nazev = $tym->findById($item['tym_id'])->fetch();
			$tym_assoc[$item['id']] = $tmp_tym_nazev['nazev'];
		}
		
		return $tym_assoc;
	}
	function getValuesZapas($form) {
		$zapas_kola = $form["zapas_kola_id"]->getValue();
		return dibi::query("SELECT [*] FROM dbo.Zapas WHERE zapas_kola_id=%i", $zapas_kola)->fetchPairs("id", "cislo_zapasu");
	}	
	function getValuesLide($form) {
		$lide = new LideModel();
		$soutez_id = $form["soutez_id"]->getValue();
		$tmp_rozhodci_soutez = dibi::query("SELECT [*] FROM dbo.Rozhodci_Soutez WHERE soutez_id=%i", $soutez_id)->fetchAll();
		$lide_assoc = array();
		foreach($tmp_rozhodci_soutez as $item){
			$tmp_lide = $lide->findById($item['lide_id'])->fetch();
			$lide_assoc[$item['id']] = $tmp_lide['jmeno'].' '.$tmp_lide['prijmeni'];
		}
		return $lide_assoc;
	}		
}

